<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class T_category extends MY_Model{
	public function __construct()
	{
		parent::__construct();
		$this->table_name = T_CATEGORY;
	}

	/**
	 * get total record
	 * @return [type] [description]
	 */
	public function get_total_rows(){
		$number = parent::get_total_rows($this->table_name);
		return $number;
	}

	/**
	 * get user by id
	 * @param  string  $select : fields of table need get
	 * @param  integer $id     id of user
	 * @return array|null
	 */
	public function get_data_by_id($id = -1, $select = '*'){
		if($id == -1 || !is_numeric($id)){
			return null;
		}

		if(strcmp($select, '*') == 0){
			$this->db->select();
		}else{
			$this->db->select($select);
		}

		$this->db->where(CATEGORY_ID ." = $id LIMIT 1");

		$result = $this->db->get($this->table_name);
		$result = $result->result_array();
		return $result;
	}

	/**
	 * get data by property
	 * @param  string $select : fields of table need get
	 * @param  array  $where  conditions
	 * @return array|null
	 */
	public function get_data_by_property($where = array(), $select = '*'){
		if($where == null || !is_array($where)){
			return null;
		}

		// query
		if(strcmp($select, '*') == 0){
			$this->db->select();
		}else{
			$this->db->select($select);
		}
		$this->db->where($where);
		$result = $this->db->get($this->table_name);
		$result = $result->result_array();
		return $result;
	}

	/**
	 * insert data into talbe
	 * @param array   $data   array data insert
	 * @param integer $result if 0 return id inserted else 1 return data user inserted
	 * @return null|integer|array
	 */
	public function set_data($data = array(), $result = 0){
		if($data == null || !is_array($data)){
			return null;
		}

		// insert rows
		$this->db->insert($this->table_name, $data);
		// get id
		$last_id = $this->db->insert_id();
		//return
		if($result == 0){
			return $last_id;
		}else{
			return $this->get_data_by_id($last_id);
		}
	}

	/**
	 * update data by id
	 * @param  array   $data : data update
	 * @param  integer $id
	 * @return integer
	 */
	public function update_data_by_id($data = array(), $id = -1){
		if($data == null || !is_array($data) || $id == -1 || !is_numeric($id)){
			return null;
		}

		//condition
		$this->db->where(CATEGORY_ID ." = $id LIMIT 1");
		$this->db->update($this->table_name, $data);
		return $this->db->affected_rows();
	}

	/**
	 * update data
	 * @param  array  $data
	 * @param  array  $where : conditions update
	 * @return integer
	 */
	public function update_data_by_property($data = array(), $where = array()){
		if($data == null || !is_array($data) || $where == null || !is_array($where)){
			return null;
		}

		$this->db->where($where);
		$this->db->update($this->table_name, $data);
		return $this->db->affected_rows();
	}

	/**
	 * [get_all description]
	 * @return [type] [description]
	 */
	public function get_list_category(){
		$this->db->select();
		$result = $this->db->get($this->table_name);
		$result = $result->result_array();
		return $result;
	}

	/**
	 * get list tag infor by category id
	 * @param  integer $caterogy_id [description]
	 * @return [type]               [description]
	 */
	public function get_list_tag_infor_by_id($category_id = -1){
		if($category_id == -1 || !is_numeric($category_id)){
			return null;
		}

		$this->db->select();
		$this->db->from(T_CAT_REQUIRED_F);
		$this->db->where(T_CAT_REQUIRED_F.'.'.CAT_RE_FIELD_CATE_ID, $category_id);
		$this->db->join(T_TAG, T_TAG.'.'.TAG_ID . ' = ' . T_CAT_REQUIRED_F.'.'. CAT_RE_FIELD_TAG_ID);
		$this->db->order_by(CAT_RE_FIELD_ORDER, 'ASC');
		$query = $this->db->get();
		$query = $query->result_array();
		return $query;
	}

	public function get_list_category_level()
	{
		$this->db->distinct();
		$this->db->select(CATEGORY_LEVEL);
		$this->db->from($this->table_name);
		$this->db->order_by(CATEGORY_LEVEL, 'ASC');
		$arrLevel = $this->db->get();
		$arrLevel = $arrLevel->result_array();
		return $arrLevel;
	}

	public function get_category_by_level($level = ''){
		$arrLevel = $this->get_list_category_level();
		if($level == '' || !is_numeric($level)){
			$level = $arrLevel[0][CATEGORY_LEVEL];
		}

		$categoryByLevel = $this->get_data_by_property(array(CATEGORY_LEVEL => $level));
		return $categoryByLevel;
	}
}